<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!-- 角色数据表 -->
<div style="float: left">
	<table id="roletable"></table>
</div>
<!-- 资源 -->
<div style="float: left; margin-left: 10px;">
	<div id="treepanel" style="padding: 10px;">
		<ul id="resourcetree"></ul>
	</div>
</div>
<!-- 说明 -->
<div style="float: left">
	<div id="tippanel" style="padding: 10px;">
		<p style="font-size: 14px;color:green;">角色授权步骤</p>
		<ul>
			<li style="color:green;margin-top:5px;">勾选需要授权的角色</li>
			<li style="color:green;margin-top:5px;">勾选对应的资源并点击确定</li>
			<li style="color:green;margin-top:5px;">系统超级管理员不需要授权，默认拥有全部权限</li>
		</ul>
	</div>
</div>
<script>
	$(document).ready(function() {
		//初始化treepanel
		initTreePanel();
		//初始化角色表数据
		initRoleTableData();
		//初始化资源树
		initResourceTree();
	})
	//初始化角色表数据
	function initRoleTableData() {
		$('#roletable').datagrid({
			title : '角色列表',
			nowrap : true,
			striped : true,
			width : 400,
			pagination : true,
			singleSelect : true,
			rownumbers : true,
			url : basePath + 'system/getRolesByPageAndSize',
			onClickRow : function(rowIndex, rowData) {
				//初始化当前角色已经有的权限
				initCurrentRoleResource();
			},
			toolbar : [ {
				text : '清空角色权限',
				iconCls : 'icon-remove',
				handler : function() {
					clearRoleResource();
				}
			} ],
			columns : [ [ {
				field : 'ck',
				checkbox : true
			}, {
				field : 'id',
				title : 'ID',
				width : 100,
				align : "center",
				hidden : true
			}, {
				field : 'name',
				title : '角色名',
				width : 300,
				align : "center",
				formatter : function(value, rowData, rowIndex) {
					var str = value;
					if (rowData.code == -1) {
						str = "<label style='color:red;'>" + value + "</label>"
					}
					return str;
				}
			} ] ]
		});
	}
	//初始化treepanel
	function initTreePanel() {
		$('#treepanel').panel({
			width : 300,
			title : '资源树',
			height : 600,
			tools : [ {
				iconCls : 'icon-ok',
				handler : function() {
					addRoleResource();
				}
			} ]
		});
	}
	//初始化资源树
	function initResourceTree() {
		$('#resourcetree').tree({
			url : basePath + 'system/getResourceTreeData',
			animate : true,
			checkbox : true
		});
	}
	//增加角色授权 
	function addRoleResource() {
		var selections = $('#roletable').datagrid('getSelections');
		var selectedNodes = $('#resourcetree').tree("getChecked");
		var roleIds = [];
		var resourceIds = [];
		if (selections.length == 0) {
			showRightMsg('请勾选角色');
			return;
		}
		 if (selectedNodes.length == 0) {
			showRightMsg('请勾选资源');
			return;
		} 
		for (var i = 0; i < selections.length; i++) {
			roleIds.push(selections[i].id);
		}
		for (var i = 0; i < selectedNodes.length; i++) {
			resourceIds.push(selectedNodes[i].id);
		}
		for (var i = 0; i < resourceIds.length; i++) {
			getParentNodeId(resourceIds[i]);
		}
		for (var i = 0; i < parentIds.length; i++) {
			if (!contains(resourceIds, parentIds[i])) {
				resourceIds.push(parentIds[i]);
			}
		}
		$.ajax({
			url : basePath + "system/addRoleResource",
			data : {
				roleIds : roleIds,
				resourceIds : resourceIds
			},
			type : "post",
			dataType : "json",
			success : function(result) {
				if (result.flag) {
					showRightMsg('操作成功');
				} else {
					showRightMsg('操作失败');
				}
			}
		})
	}
	//根据当前TreeID获取其父节点
	var parentIds = [];
	function getParentNodeId(id) {
		var node = $('#resourcetree').tree('find', id);
		var parent = $('#resourcetree').tree('getParent', node.target);
		if (parent) {
			parentIds.push(parent.id);
			getParentNodeId(parent.id);
		}
	}
	//初始化当前点击角色的权限
	function initCurrentRoleResource() {
		//取消已经选择的 uncheck
		var nodes = $('#resourcetree').tree('getChecked');
		for (var i = 0; i < nodes.length; i++) {
			$('#resourcetree').tree('uncheck', nodes[i].target);
		}
		var selection = $('#roletable').datagrid('getSelected');
		if (selection) {
			$.ajax({
				url : basePath + "system/initCurrentRoleResource",
				data : {
					id : selection.id
				},
				type : "post",
				dataType : "json",
				success : function(result) {
					for (var i = 0; i < result.length; i++) {
						var node = $('#resourcetree')
								.tree('find', result[i].id);
						var r = $('#resourcetree').tree('isLeaf', node.target);
						//当节点是叶子节点才选择
						if (r) {
							$('#resourcetree').tree('check', node.target);
						}
					}
				}
			})
		}
	}
	//清空角色权限
	function clearRoleResource(){
		var selection = $('#roletable').datagrid('getSelected');
		var roleIds = [];
		roleIds.push(selection.id);
		if (selection) {
			$.messager.confirm('确认', '你确定清空该角色权限吗?', function(r) {
				if (r) {
					$.ajax({
						url : basePath + "system/clearRoleResource",
						data : {
							roleIds : roleIds
						},
						type : "post",
						dataType : "json",
						success : function(result) {
							if (result.flag) {
								showRightMsg('操作成功');
								$('#resourcetree').tree("reload");
							} else {
								showRightMsg('操作失败');
							}
						}
					})
				}
			});
		}else{
			showRightMsg('请选择需要清空权限的角色');	
		}
	}
</script>